Dynamic memory management

From Cppreference

< cpp
Jump to: navigation, search

Contents

[edit] Low level memory management

[edit] Allocators

Allocators are class templates encapsulating memory allocation strategy. This allows generic containers to decouple memory management from the data itself.

Defined in header <memory>
allocator
The default allocator. Before C++11 any allocator had to provide
the interface (but not necessarily implementation), identical
to one of the default allocator, also it could not have nonstatic
data. In C++11 these two rules are relaxed, allocators
can be implemented through allocator_traits
(class template)
allocator_traits (C++11)
Provides standard interface to access the interface and properties of particular allocator
(class template)
Defined in header <scoped_allocator>
scoped_allocator_adaptor (C++11)
Implements multi-level allocator, which can be used in multi-level
containers to specify allocator for elements in each particular level
(class template)

[edit] Uninitialized storage

Several utilities are provided to initialize raw storage

Defined in header <memory>
uninitialized_copy
copies a range of objects to an uninitialized area of memory
(function template)
uninitialized_copy_n (C++11)
copies a number of objects to an uninitialized area of memory
(function template)
uninitialized_fill
copies an object to an uninitialized area of memory
(function template)
uninitialized_fill_n
copies an object to an uninitialized area of memory
(function template)

[edit] Smart pointers

Smart pointers enable easier and less error prone object lifetime management.

Defined in header <memory>
Unique pointers
unique_ptr (C++11)
provides pointer ownership semantics, i.e. only one unique_ptr object can own the given object,
which is destroyed when the unique_ptr object referring to it is destroyed
(class template)
default_delete (C++11)
default deleter for unique_ptr class template
(class template)
Shared pointers
shared_ptr (C++11)
provides shared pointer ownership semantics, i.e. multiple shared_ptr instances can manages an object,
which is destroyed when the last shared_ptr object referring to it is destroyed
(class template)
weak_ptr (C++11)
holds weak reference to an object already managed by shared_ptr, i.e. the object can be destroyed
even there is weak_ptr instance referring to it
(class template)
enable_shared_from_this (C++11)
allows an object to get a shared_ptr referring to itself
(class template)
bad_weak_ptr (C++11)
an exception that is thrown when accessing a weak_ptr which refers to already destroyed object
(class template)
auto_ptr
auto_ptr (deprecated)
provides unique pointer ownership semantics. This class is deprecated, use unique_ptr instead.
(class template)

[edit] shared_ptr atomic access

Overloads of all atomic functions are provided.

[edit] shared_ptr hash support

The following specialization of std::hash is provided.

Defined in header <memory>

template<> class hash<std::shared_ptr>;

[edit] Garbage collector support

Defined in header <memory>
declare_reachable (C++11)
declares that an object can not be recycled even if there is no pointer referring to it
(function template)
undeclare_reachable (C++11)
declares that an object can not be recycled even if there is no pointer referring to it
(function template)
declare_no_pointers (C++11)
declares that a memory area does not contain pointers
(function template)
undeclare_no_pointers (C++11)
declares that a memory area can have pointers
(function template)
pointer_safety (C++11)
returns the implementation's pointer safety
(function template)

[edit] Miscellaneous

Defined in header <memory>
pointer_traits (C++11)
provides interface to query information about several properties of pointer-like types
(class template)
address_of (C++11)
obtains actual address of an object, even if the & operator is overloaded
(function template)
align (C++11)
aligns a pointer in a buffer
(function template)

[edit] C library

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages